package com.power.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.power.ov.ResultDataTable;
import com.power.pojo.Tower;
import com.power.service.TowerService;
import com.power.service.impl.TowerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: 李辽
 * Date: 2020/10/04 15:22
 * Description:
 * Version: V1.0
 */
@WebServlet("/ChangeTower.do")
public class ChangeTower extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");

        //获取前端传来的数据
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Tower tower = JSONObject.parseObject(s,Tower.class);//将数据转换为tower对象
        System.out.println("进入杆塔编辑后台，数据为"+tower.toString());

        ResultDataTable resultData = new ResultDataTable();//创建一个返回对象

        TowerService towerService = new TowerServiceImpl();
        int result = 0;
        try {
            result = towerService.ChangeTower(tower);
            System.out.println(result);
            if (result>0){
                resultData.setCode(200);
                resultData.setMessage("更改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            resultData.setCode(500);
            resultData.setMessage("修改失败！！！");
        }
        response.getWriter().write(JSON.toJSONString(resultData));
        response.getWriter().flush();
        response.getWriter().close();

    }
}


























